package com.wdc.deliveryrobot.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.Sp.SPManager;
import com.wdc.deliveryrobot.dialog.AddPointDialog;
import com.wdc.deliveryrobot.dialog.HintDialog;
import com.wdc.deliveryrobot.event.Event;
import com.wdc.deliveryrobot.model.PointOne;
import com.wdc.deliveryrobot.view.MapEditView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class DrawActivity extends BaseActivity implements View.OnClickListener{

    private JSONObject jsonObjectScan;

    private MapEditView mapEditView;

    private Button buttonSave,buttonCancel;
    private List<PointOne> pointOnes;
    private Gson gson;

    private LinearLayout linearLayout;

    private Handler handler = new Handler(Looper.getMainLooper());

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //子线程
                    jsonObjectScan = selfChassisState.scan;
                    if (SPManager.getPoint(getApplicationContext()) == null){
                        pointOnes = new ArrayList<>();
                    }else {
                        pointOnes = gson.fromJson(SPManager.getPoint(getApplicationContext()),new TypeToken<List<PointOne>>() {}.getType());
                    }
                    //在主线程中更新ui
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            mapEditView.setLaserData(jsonObjectScan,selfChassisState.getMapData(), pointOnes);

                        }
                    });
                }
            }).start();
            //每1秒执行一次
            handler.postDelayed(this,1000);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw);
        mapEditView = findViewById(R.id.editView);
        linearLayout = findViewById(R.id.return_back);
        buttonSave = findViewById(R.id.saveMap);
        buttonCancel = findViewById(R.id.cancelMap);
        gson = new Gson();
        linearLayout.setOnClickListener(this);
        buttonSave.setOnClickListener(this);
        buttonCancel.setOnClickListener(this);
        handler.post(runnable);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.return_back:
                finish();
                break;
            case R.id.saveMap:
                AddPointDialog addPointDialog = new AddPointDialog(DrawActivity.this);
                addPointDialog.setHint(getString(R.string.hintmapname));
                addPointDialog.setMessage(getString(R.string.mapname));
                addPointDialog.setTop(getString(R.string.topname));
                addPointDialog.pointSet();
                addPointDialog.setWidth(500,600);
                addPointDialog.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (addPointDialog.isEditNull() || addPointDialog.isMapNull()){
                            Toast.makeText(getApplicationContext(),getString(R.string.pointNull),Toast.LENGTH_SHORT).show();
                        }else {
                            String name = addPointDialog.getEditText();
                            Log.d("thd_","地图名称:" + name + "地图楼层:" + addPointDialog.getEditMap());
                            addPointDialog.dismiss();
                        }

                    }
                });

                addPointDialog.show();
                break;
            case R.id.cancelMap:
                HintDialog hintDialog = new HintDialog(DrawActivity.this);

                hintDialog.setMessage(getString(R.string.yesexit));
                hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
                hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hintDialog.dismiss();
                    }
                });
                hintDialog.show();
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnDataEvent(Event.OnDataEvent onDataEvent){

    }
}